package com.hepinghospital.user.ui.activity.userinfo;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyf.immersionbar.ImmersionBar;
import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;

import butterknife.BindView;
import butterknife.OnClick;
import jz.joyoung.robot.util.AES128;
import jz.joyoung.robot.util.UtilString;


/**
 * 注册
 *
 * @author DELL
 */

public class RegisterActivity extends BaseActivity {
    @BindView(R.id.edt_name)
    EditText edtName;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.edt_phone)
    EditText edtPhone;
    @BindView(R.id.edt_password)
    EditText edtPassword;
    @BindView(R.id.edt_password_again)
    EditText edtPpasswordAgain;
    @BindView(R.id.btn_code)
    TextView btnCode;
    @BindView(R.id.edt_code)
    EditText edtCode;
    @BindView(R.id.iv_password_hide)
    ImageView ivPasswordHide;
    @BindView(R.id.iv_password_again_hide)
    ImageView ivPasswordAgainHide;

    private boolean isShowPassword, isShowPasswordAgain = false;

    private Handler mHandler = new Handler();

    int second = 60;

    @Override
    protected void initViews() {
        tvTitle.setText("注册");
    }

    @Override
    protected void initData() {
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_register;
    }

    @Override
    protected void doBeforeSetcontentView() {
        ImmersionBar.with(this).keyboardEnable(false).transparentStatusBar().fitsSystemWindows(false).statusBarDarkFont(true).navigationBarColor(R.color.black).autoDarkModeEnable(true).init();
    }

    @OnClick({R.id.btn_back, R.id.btn_code, R.id.btn_register, R.id.ll_toLogin, R.id.iv_password_hide, R.id.iv_password_again_hide})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
            case R.id.ll_toLogin:
                finish();
                break;
            case R.id.btn_code:
                if (UtilString.isEmpty(edtPhone.getText().toString()) || !UtilString.isPhoneNumNo(edtPhone.getText().toString())) {
                    ToastUtil.showToastCenter(mContext, "请输入正确的手机号");
                    return;
                }
                second = 60;
                getCode();
                break;
            case R.id.btn_register:
                register();
                break;
            case R.id.iv_password_hide:
                if (isShowPassword) {
                    ivPasswordHide.setImageResource(R.mipmap.icon_password_open);
                    edtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    ivPasswordHide.setImageResource(R.mipmap.icon_password_hide);
                    edtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                isShowPassword = !isShowPassword;
                break;

            case R.id.iv_password_again_hide:
                if (isShowPasswordAgain) {
                    ivPasswordAgainHide.setImageResource(R.mipmap.icon_password_open);
                    edtPpasswordAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    ivPasswordAgainHide.setImageResource(R.mipmap.icon_password_hide);
                    edtPpasswordAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                isShowPasswordAgain = !isShowPasswordAgain;
                break;

            default:
                break;
        }
    }

    private void getCode() {
        btnCode.setEnabled(false);

        mHandler.postDelayed(runnable, 1000);
        try {
            RestProxy.getInstance().getCode(AES128.encryptStringAES(edtPhone.getText().toString(), "7t3e506jaa10xbd4"), new BaseObserver<String>() {
                @Override
                public void onError(Throwable e) {
                    ToastUtil.onError(RegisterActivity.this, e);
                }

                @Override
                public void onSuccess(String result) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void register() {
        if (UtilString.isEmpty(edtPhone.getText().toString()) || !UtilString.isPhoneNumNo(edtPhone.getText().toString())) {
            ToastUtil.showToastCenter(mContext, "请输入正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(edtName.getText().toString())) {
            ToastUtil.showToast(mContext, "请输入姓名");
            return;
        }

        if (TextUtils.isEmpty(edtCode.getText().toString())) {
            ToastUtil.showToast(mContext, "验证码为空");
            return;
        }

        if (TextUtils.isEmpty(edtPassword.getText().toString())) {
            ToastUtil.showToast(mContext, "请设置登录密码");
            return;
        }
        if (TextUtils.isEmpty(edtPpasswordAgain.getText().toString())) {
            ToastUtil.showToast(mContext, "请再次输入密码");
            return;
        }

        if (!edtPassword.getText().toString().equals(edtPpasswordAgain.getText().toString())) {
            ToastUtil.showToast(mContext, "两次输入密码不一致");
            return;
        }
        try {
            RestProxy.getInstance().register(edtName.getText().toString(), "", AES128.encryptStringAES(edtPassword.getText().toString(), "7t3e506jaa10xbd4"), edtCode.getText().toString(), AES128.encryptStringAES(edtPhone.getText().toString(), "7t3e506jaa10xbd4"), "", "", "", "", new BaseObserver<String>() {
                @Override
                public void onError(Throwable e) {
                    ToastUtil.onError(RegisterActivity.this, e);

                }

                @Override
                public void onSuccess(String result) {
                    ToastUtil.showToast(mContext, "注册成功");
                    finish();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacks(runnable);
    }

    private Runnable runnable = new Runnable() {
        @SuppressLint("SetTextI18n")
        @Override
        public void run() {
            second--;
            if (second != 0) {
                mHandler.postDelayed(runnable, 1000);
                btnCode.setEnabled(false);
                btnCode.setText(second + "秒后重试");
            } else {
                btnCode.setText("发送验证码");
                btnCode.setEnabled(true);
            }
        }
    };
}
